<template>
  <div ref="pieRef" style="width: 100%; height: 100%" />
</template>

<script>
import * as echarts from 'echarts'
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_color.js'
import ResizeObserver from 'resize-observer-polyfill'
export default {
  data() {
    return {
      chartInstance: null,
      option: {
        color: ['#21ffeb', '#fffff'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          itemWidth: 20,
          itemHeight: 1,
          top: '0%',
          left: '0%',
          textStyle: {
            // color: "rgba(255,255,255,0.8)",
          }
        },

        grid: {
          left: '1%',
          right: '1%',
          bottom: '0%',
          top: '18%',
          containLabel: true
        },
        xAxis: {
          axisLabel: {
            color: 'rgba(0,0,0,.45)'
          },
          // boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(0,0,0,.45)'
            }
          },
          type: 'category',
          data: [2, 4]
        },
        yAxis: {
          axisLabel: {
            color: 'rgba(0,0,0,.4)'
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitArea: {
            show: false
          },
          minInterval: 1,
          type: 'value'
        },

        series: [
          {
            name: '日志',
            type: 'line',
            smooth: true,
            itemStyle: {
              color: '#21ffeb'
            },
            lineStyle: {
              normal: {
                width: 2,
                color: 'rgba(83, 182, 240,1)' // 线条颜色
              }
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.6,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#21ffeb'
                },
                {
                  offset: 1,
                  color: '#ffffff'
                }
              ])
            },
            emphasis: {
              focus: 'series'
            },
            data: [0]
          }
        ]
      }
    }
  },
  computed: {
    ...mapState({
      sideTheme: (state) => state.settings.sideTheme
    })
  },
  watch: {
    sideTheme: {
      handler() {
        this.$nextTick(() => {
          this.updateChart()
        })
      },
      immediate: true
    }
  },
  mounted() {
    this.initChart()
    const resizeOb = new ResizeObserver((entries) => {
      for (const entry of entries) {
        echarts.getInstanceByDom(entry.target)?.resize()
      }
    })
    resizeOb.observe(this.$refs.pieRef)

    // 添加随页面变化而变化
    window.addEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  destroyed() {
    window.removeEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  methods: {
    initChart() {
      // 初始化对象
      this.chartInstance = echarts.init(this.$refs.pieRef)
      this.chartInstance.setOption(this.option, true)
      window.addEventListener('resize', () => {
        this.chartInstance.resize()
      })
    },
    getData(searchType, params) {
      const xData = []
      const data = []
      params.forEach(item => {
        if (searchType === '1') {
          xData.push(item.hourTime)
        } else {
          xData.push(item.dayTime)
        }
        data.push(item.num)
      })
      if (params.length === 1) {
        this.option.series[0].type = 'bar'
        this.option.series[0].barWidth = 30
        this.option.series[0].itemStyle = {
          normal: {
            barBorderRadius: [4, 4, 0, 0],
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: '#21ffeb'
              },
              {
                offset: 1,
                color: '#ffffff'
              }
            ])
          }
        }
      } else {
        this.option.series[0].type = 'line'
      }

      this.option.xAxis.data = xData
      this.option.series[0].data = data
      this.chartInstance.setOption(this.option, true)
      this.updateChart()
    },
    updateChart() {
      const titleThemeDark = getThemeValue('titleThemeDark')
      const DarkThemeStyle = {
        title: {
          textStyle: {
            color: titleThemeDark.titleColor
          }
        },
        legend: {
          textStyle: {
            color: titleThemeDark.titleColor
          }
        },
        xAxis: {
          axisLabel: {
            color: titleThemeDark.titleColor
          },
          axisLine: {
            show: false
          }
        },
        yAxis: {
          axisLabel: {
            color: titleThemeDark.titleColor
          },
          splitLine: {
            lineStyle: {
              color: ' rgba(0,0,0,0.15)'
            }
          },
          axisLine: {
            show: false
          }
        }
      }
      const titleThemeHigh = getThemeValue('titleThemeHigh')
      const HighThemeStyle = {
        title: {
          textStyle: {
            color: titleThemeHigh.titleColor
          }
        },
        legend: {
          textStyle: {
            color: titleThemeHigh.titleColor
          }
        },
        xAxis: {
          axisLabel: {
            color: titleThemeHigh.titleColor
          },
          axisLine: {
            show: false
          }
        },
        yAxis: {
          axisLabel: {
            color: titleThemeHigh.titleColor
          },
          axisLine: {
            show: false
          }
        }
      }
      if (this.sideTheme === 'theme-dark' || this.sideTheme === 'tech') {
        this.chartInstance.setOption(DarkThemeStyle)
      } else {
        this.chartInstance.setOption(HighThemeStyle)
      }
    }
  }
}
</script>

        <style lang="scss" scoped>
.visit-app {
  width: 100%;
  height: 100%;
}
</style>

